package com.weixingtu.commuterCar.servlet;

import com.alibaba.fastjson.JSONObject;
import com.weixingtu.commuterCar.bean.Line;
import com.weixingtu.commuterCar.bean.Message;
import com.weixingtu.commuterCar.service.LineService;
import com.weixingtu.commuterCar.service.impl.LineServiceImpl;
import com.weixingtu.commuterCar.tool.gps.impl.WWGps;
import com.weixingtu.commuterCar.util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@WebServlet(urlPatterns = "/getLocation.do")
public class CarServlet extends HttpServlet {
    private LineService lineService = new LineServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Line> loctionList = lineService.queryLocation();
//        System.out.println(loctionList == null || loctionList.size() == 0 || System.currentTimeMillis() - loctionList.get(0).getDateTime() > 6000);
//        if (loctionList == null || loctionList.size() == 0 || System.currentTimeMillis() - loctionList.get(0).getDateTime() > 6000) {
//            List<JSONObject> positionList = getCarLocation();
//            loctionList = saveCarLoction(positionList);
//        }
        List<JSONObject> positionList = getCarLocation();
        loctionList = saveCarLoction(positionList);
        Message msg = new Message();
        msg.setData(loctionList);
        String json = JSONUtil.toJSON(msg);
        PrintWriter writer = response.getWriter();
        writer.print(json);
    }


    /**
     * 保存车位置信息
     *
     * @param positionList
     */
    private List<Line> saveCarLoction(List<JSONObject> positionList) {
        Iterator<JSONObject> iterator = positionList.iterator();
        List<Line> loctionList = new ArrayList<>();
        int flag = 0;
        while (iterator.hasNext()) {
            JSONObject lineObj = iterator.next();
            Line line = new Line();
            line.setLineId(Integer.parseInt(lineObj.getString("lineId")));
            line.setLng(Double.parseDouble(lineObj.getString("lng")));
            line.setLat(Double.parseDouble(lineObj.getString("lat")));
            line.setSpeed(Double.parseDouble(lineObj.getString("speed")));
            line.setDateTime(lineObj.getLong("time"));
            Date date = new Date(lineObj.getLong("time")*1000L);
            java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            String dateString = simpleDateFormat.format(date);
            line.setDateString(dateString);

            loctionList.add(line);
            if(!"离线".equals(lineObj.getString("status"))) {
                flag = 1;
            }
        }

        if(flag==1) {//车辆gps状态都为离线时位置数据不插入数据库,否则插入
            for (Line line : loctionList) {
                lineService.insert(line);
            }
        }
        return loctionList;
    }

    /**
     * 获取车辆gps位置信息
     *
     * @return
     */
    private List<JSONObject> getCarLocation() {
        WWGps api = new WWGps();
        api.init();
        List<JSONObject> positionList = api.getPosition();
        return positionList;
    }

}
